package com.zszq.ms.bigmodeltest.service.impl;

import com.zszq.ms.bigmodeltest.domain.response.Result;
import com.zszq.ms.bigmodeltest.entity.AiBigmodelConfig;
import com.zszq.ms.bigmodeltest.entity.AiTask;
import com.zszq.ms.bigmodeltest.entity.AiTaskResult;
import com.zszq.ms.bigmodeltest.enums.SubTaskStatusEnum;
import com.zszq.ms.bigmodeltest.mapper.AiTaskMapper;
import com.zszq.ms.bigmodeltest.mapper.AiTaskResultMapper;
import com.zszq.ms.bigmodeltest.service.AiBigmodelConfigService;
import com.zszq.ms.bigmodeltest.service.AiTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zszq.ms.bigmodeltest.task.TaskUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ms
 * @since 2025-03-31
 */
@Service
public class AiTaskImpl extends ServiceImpl<AiTaskMapper, AiTask> implements AiTaskService {
    @Autowired
    private TaskUtils taskUtils;

    @Autowired
    private AiBigmodelConfigService bigmodelConfigService;

    @Autowired
    private AiTaskResultMapper aiTaskResultMapper;

    @Override
    public Result executeTask(String id) {
        AiTask aiTask = baseMapper.selectById(id);
        return taskUtils.executeTask(aiTask);
    }

    @Override
    public int add(AiTask aiTask) {
        String id = UUID.randomUUID().toString();
        aiTask.setId(id);
        String modelConfigIds = aiTask.getBigModelConfigIds();
        String[] split = modelConfigIds.split(",");
        for (String modelConfigId : split) {
            AiTaskResult aiTaskResult = new AiTaskResult();
            aiTaskResult.setId(UUID.randomUUID().toString());
            aiTaskResult.setBigModelConfigId(modelConfigId);
            aiTaskResult.setTaskId(id);
            aiTaskResult.setStatusCode(SubTaskStatusEnum.AWAIT_RUN.getCode());
            aiTask.setStatusName(SubTaskStatusEnum.AWAIT_RUN.getValue());
            aiTaskResultMapper.insert(aiTaskResult);
        }
       return baseMapper.insert(aiTask);
    }

    @Override
    public int update(AiTask aiTask) {
       return baseMapper.updateById(aiTask);
    }
}
